package com.icoding.fw.entities;

import java.math.BigInteger;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.persistence.Version;

@MappedSuperclass
public abstract class BaseDom {

	private static final long serialVersionUID = 1L;
	public static final String PROPERTY_ID = "id";
	protected static final String COLUMN_ID = "ID";

	@Id
	@Column(name = COLUMN_ID, nullable = false, updatable = false)
	@GeneratedValue(strategy = GenerationType.AUTO)
	private BigInteger id;

	@Version
	@Column(name = "version")
	private Long version;

	@Transient
	private Date lastUpdated;

	@Transient
	private Date createDated;

	public Date getCreateDated() {
		return createDated;
	}

	public void setCreateDated(Date createDated) {
		this.createDated = createDated;
	}

	public BigInteger getId() {
		return id;
	}

	public void setId(BigInteger id) {
		this.id = id;
	}

	public Long getVersion() {
		return version;
	}

	public void setVersion(Long version) {
		this.version = version;
	}

	public Date getLastUpdated() {
		return lastUpdated;
	}

	public void setLastUpdated(Date lastUpdated) {
		this.lastUpdated = lastUpdated;
	}

	protected void copy(final BaseDom source) {
		this.id = source.id;
		this.version = source.version;
		this.lastUpdated = source.lastUpdated;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof BaseDom)) {
			return false;
		}
		final BaseDom bd = (BaseDom) obj;
		if (this.id != null && bd.id != null) {
			if (this.id != bd.id) {
				return false;
			}
		}
		return true;
	}

	protected static boolean getBooleanValue(final Boolean value) {
		return Boolean.valueOf(String.valueOf(value));
	}

	public void removeIdentity() {
		id = null;
		lastUpdated = null;
		createDated = null;
	}

}
